home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.07 Jul 91 / MacLock 1.0 / CPassword.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-05  |  2.5 KB  |  140 lines  |  [TEXT/KAHL]

  1. /*****
  2.  * FILE:        CPassword.c
  3.  * Programmer:    Mark Bykerk Kauffman
  4.  * Date:        1/90
  5.  * Purpose:
  6.  *        The CPassword class methods.        
  7.  *         Copyright © 1990 Mark Bykerk Kauffman. All rights reserved.
  8.  * SUPERCLASS = CObject    
  9.  *
  10.  *****/
  11.  
  12. #include "CPassword.h"
  13. #include <string.h>    /* Use the ANSI strcmp function in WaitForPassowrd. */
  14.  
  15. /*** Class Constants ***/
  16. #define PASSWORD_DLOG_ID    2048
  17. #define    NIL_POINTER            0L
  18. #define    MOVE_TO_FRONT        -1L
  19. #define OK_BUTTON            1
  20. #define TEXT_BOX            3
  21.  
  22. /**** C O N S T R U C T I O N / D E S T R U C T I O N   M E T H O D S ****/
  23.  
  24.  
  25. /***
  26. * IPassword
  27. *
  28. *        Initialize a Password object
  29. ***/
  30.  
  31. void    CPassword::IPassword()
  32. {
  33.     aPassword[0]='\0';
  34. }
  35.  
  36.  
  37. /***
  38. * Dispose {OVERRIDE}
  39. *
  40. *        Dispose of Password by releasing all its resources
  41. ***/
  42.  
  43. void    CPassword::Dispose()
  44. {
  45.     inherited::Dispose();  
  46. }
  47.  
  48.  
  49. /***
  50. * ChangePassword
  51. *
  52. *        Display a Dialog Box that lets the user change the instance variable
  53. *        aPassword.
  54. ***/
  55.  
  56. void     CPassword::ChangePassword()
  57. {
  58.     DialogPtr        passwordDialog;
  59.     int                itemHit, dialogDone = FALSE;
  60.     int                itemType;
  61.     Rect            itemRect;
  62.     Handle            itemHandle;
  63.     char            *PtPassword;
  64.  
  65.     PtPassword = this->aPassword;
  66.     CtoPstr(PtPassword);
  67.     passwordDialog = GetNewDialog(PASSWORD_DLOG_ID,NIL_POINTER,
  68.                                                     MOVE_TO_FRONT);
  69.     GetDItem(passwordDialog,3,&itemType,&itemHandle,&itemRect);
  70.     SetIText(itemHandle,PtPassword);
  71.     SelIText(passwordDialog,3,0,32767);
  72.          
  73.     while ( dialogDone == FALSE )
  74.     {
  75.         ModalDialog(NIL_POINTER, &itemHit);
  76.         switch ( itemHit )
  77.         {
  78.             case     OK_BUTTON:
  79.                     dialogDone = TRUE;
  80.                     break;
  81.         }
  82.     }
  83.     GetIText(itemHandle,&aPassword);
  84.     PtoCstr(PtPassword);
  85.     DisposDialog(passwordDialog); 
  86. }
  87.  
  88. /***
  89. * WaitForPassword
  90. *
  91. *        Waits for the user to type in thePassword followed by a carriage return.
  92. ***/
  93.  
  94. void    CPassword::WaitForPassword()
  95. {
  96.     int                i=0;
  97.     char            theGuess[255];
  98.     char            *ptTheGuess,charCode;
  99.     EventRecord        theEvent;
  100.     char            *PtPassword;
  101.     
  102.     Boolean            done;
  103.     Boolean            endOfLine;
  104.     
  105.     PtPassword = this->aPassword;    
  106.     ptTheGuess = theGuess;
  107.     done = FALSE;
  108.     
  109.     do
  110.     {
  111.         endOfLine = FALSE;
  112.         i=0;
  113.         theGuess[0]='\0';
  114.         FlushEvents(everyEvent, 0);
  115.         while (endOfLine==FALSE)
  116.         {
  117.             GetOSEvent(everyEvent,&theEvent);
  118.             switch (theEvent.what)
  119.             {
  120.             case keyDown:
  121.                     charCode = BitAnd(theEvent.message,charCodeMask);
  122.                     if (charCode != '\r')
  123.                     {
  124.                         theGuess[i] = charCode;
  125.                         ++i;
  126.                         theGuess[i]='\0';
  127.                     }
  128.                     else
  129.                     {
  130.                         endOfLine=TRUE;
  131.                     }
  132.                     break;
  133.             default:
  134.                     done=FALSE;
  135.             }
  136.         }
  137.         SysBeep(20);
  138.     } while (strcmp(PtPassword,ptTheGuess)!=0); 
  139. }
  140.